load 'rep_utils.rb'
load 'wechat_api.rb'

if ARGV.size < 2
    puts "Usage: change-user-spreader.rb user_uid new_spreader_uid skip_np [reason]"
    puts "Usage: change-user-spreader.rb user_uid new_spreader_uid not_skip_np [reason]"
    exit
end

uid = ARGV[0].to_i
new_spreader = ARGV[1].to_i
skip_np = ARGV[2].to_s == 'skip_np' ? true : false

reason = 'OMS审计'
reason = ARGV[3].to_s if !ARGV[3].nil?

current_spreader = 0
add_time = 0
sql = "select uid, spread_uid, add_time from tutem_mall.eb_user where uid = #{uid}"
(queryRds(sql)).each do |q|
    add_time = q['add_time']
    current_spreader = q['spread_uid']
end

if add_time == 0 || new_spreader == current_spreader
    puts "[exit] no records for uid##{uid} found" if add_time == 0
    puts "[exit] same spreader_uid found" if new_spreader == current_spreader
    exit
end

puts "change_user_spreader #{uid}, #{new_spreader}, #{Time.now.to_i}, #{skip_np}"
change_user_spreader uid, new_spreader, Time.now.to_i, skip_np

notice = "用户##{uid}经#{reason}，符合【专业用户新患间接介绍认可标准-2022Q3后】，自注册日#{Time.at(add_time).strftime('%Y-%m-%d')}起，推荐渠道由C##{current_spreader}改为C##{new_spreader}，统计标签已同步"
send_message_to_bot 'WWBOT_XT', notice, ['@all']
